#include <signal.h> 
#include <fcntl.h>
#include <unistd.h>
#include <el.h>
#include "bolt.h"

int set_nonblock(int fd)
{
   int opts = fcntl(fd, F_GETFL);
   if(opts < 0)
      return -1;
   opts |= O_NONBLOCK;
   return fcntl(fd, F_SETFL, opts);
}



static void enable_cli(const char *prompt, const char *config)
{
  cli_init(prompt, config);
  struct el_events events = {
    .in_event = cli_in_event,
    .out_event = NULL
  };
  set_nonblock(STDIN_FILENO);
  el_add(STDIN_FILENO, &events, NULL);
}

static void disable_cli(void)
{
  cli_exit();
}

static void handle_signal(int num) 
{
   el_stop();
}

static void init_signal (void)
{
  signal (SIGHUP, handle_signal);
  signal (SIGINT, handle_signal);
  signal (SIGTERM, handle_signal);
  signal (SIGQUIT, handle_signal);
  signal (SIGSEGV, handle_signal);
  signal (SIGBUS, handle_signal);
  signal (SIGTSTP, handle_signal);
  signal (SIGTTIN, handle_signal);
  signal (SIGTTOU, handle_signal);
}

int main(int argc, char *argv[])
{
  el_init();
  init_signal();
  enable_cli("boltex", "boltex.cmd");
  el_loop();
  disable_cli();
  return 0;
}




